package at.ac.tuwien.ifs.calendit.basis.gui;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableColumnModel;

import at.ac.tuwien.ifs.calendit.basis.helper.Constants;

/**
 * Mouse Listener class for Monatsansicht 
 * @author mona
 *
 */
public class JMonatsansichtMouseListener implements MouseListener {
	
	/**
	 * table which the mouse-listener is applied to;
	 */
	private JTable table;
	
	private void forwardEventToButton(MouseEvent e) {
	    TableColumnModel columnModel = table.getColumnModel();
	    int column = columnModel.getColumnIndexAtX(e.getX());
	    int row    = e.getY() / table.getRowHeight();
	    Object value;
	    JLabel button;
	    MouseEvent buttonEvent;

	    if(row >= table.getRowCount() || row < 0 ||
	       column >= table.getColumnCount() || column < 0)
	      return;

	    value = table.getValueAt(row, column);

	    if(!(value instanceof JLabel))
	      return;

	    button = (JLabel)value;
	    buttonEvent = (MouseEvent)SwingUtilities.convertMouseEvent(table, e, button);
	    button.dispatchEvent(buttonEvent);
	    // This is necessary so that when a button is pressed and released
	    // it gets rendered properly.  Otherwise, the button may still appear
	    // pressed down when it has been released.
	    table.repaint();
	  }

	
	/**
	 * creates a new Monatsansicht Mouse Listener
	 * @param table - the monatsansicht table, which this mouse listener
	 * is applied to.
	 */
	  public JMonatsansichtMouseListener(JTable table) {
	    this.table = table;
	  }

	  
	  
	  /**
	   * implements method inherited from MouseListener
	   * if a mouse is pressed on a JLabel nothing 
	   * happens, if a JButton (termin!) is pressed,
	   * tagesansicht opens with the correct date (of the 
	   * termin-button which has been clicked on). 
	   */
	  public void mousePressed(MouseEvent e) {
		 int row= table.getSelectedRow();
		 int col= table.getSelectedColumn();
		 if(JMonatsansichtTableModel.rows[row][col].getClass().toString().equals("class javax.swing.JButton")){
			 int month = Monatsansicht.currMonth;
			 int year = Monatsansicht.currYear;
			 String buttText = ((JButton)JMonatsansichtTableModel.rows[row][col]).getText();
			 String[] buttInhalt = buttText.split(">");
			 String[] split = buttInhalt[2].split("<");
			 int day = Integer.valueOf(split[0]);
			 Calendar now=new GregorianCalendar(year,month,day);
			 java.sql.Date date= new java.sql.Date(now.getTimeInMillis());
			 Constants.selectedDate = date;
			 Tagesansicht.cal.setDate(date);
			 KalenderUebersicht.changeToTagesansicht();
		 } else {
			 //eventuell noch neuen termin bei klick in jlabel..
		 }
	    forwardEventToButton(e);
	  }
	  
	  /**
	   * method inherited from MouseListener
	   */
	  public void mouseClicked(MouseEvent e) {
	    forwardEventToButton(e);
	  }

	  /**
	   * method inherited from MouseListener
	   */
	  public void mouseEntered(MouseEvent e) {
	    forwardEventToButton(e);
	  }

	  /**
	   * method inherited from MouseListener
	   */
	  public void mouseExited(MouseEvent e) {
	    forwardEventToButton(e);
	  }

	  /**
	   * method inherited from MouseListener
	   */
	  public void mouseReleased(MouseEvent e) {
	    forwardEventToButton(e);
	  }
}
